package org.dromara.Nyt7StatisticsInsureNumRegion.service.impl;

import org.dromara.Nyt7PolicyInsurance.domain.Nyt7PolicyInsurance;
import org.dromara.Nyt7PolicyInsurance.mapper.Nyt7PolicyInsuranceMapper;
import org.dromara.utils.utils.FilterUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import org.dromara.Nyt7StatisticsInsureNumRegion.domain.bo.Nyt7StatisticsInsureNumRegionBo;
import org.dromara.Nyt7StatisticsInsureNumRegion.domain.vo.Nyt7StatisticsInsureNumRegionVo;
import org.dromara.Nyt7StatisticsInsureNumRegion.service.INyt7StatisticsInsureNumRegionService;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * 统计分析_投保统计_数量统计_按地区的数量统计Service业务层处理
 *
 * @author yt
 * @date 2025-01-06
 */
@RequiredArgsConstructor
@Service
public class Nyt7StatisticsInsureNumRegionServiceImpl implements INyt7StatisticsInsureNumRegionService {

    private final Nyt7PolicyInsuranceMapper nyt7PolicyInsuranceMapper;

    /**
     * 分页查询统计分析_投保统计_数量统计_按地区的数量统计列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 统计分析_投保统计_数量统计_按地区的数量统计分页列表
     */
    @Override
    public TableDataInfo<Nyt7StatisticsInsureNumRegionVo> queryPageList(Nyt7StatisticsInsureNumRegionBo bo, PageQuery pageQuery) {

        List<Nyt7StatisticsInsureNumRegionVo> statisticsVos = getStatisticsVos(bo);
        // 创建 Page 对象
        Page<Object> build = pageQuery.build();
        // 创建 Page 对象
        Page<Nyt7StatisticsInsureNumRegionVo> page = new Page<>(build.getCurrent(),build.getSize());
        page.setRecords(statisticsVos);
        page.setTotal(statisticsVos.size());
        page.setRecords(statisticsVos);
        return TableDataInfo.build(page);
    }

    private @NotNull List<Nyt7StatisticsInsureNumRegionVo> getStatisticsVos(Nyt7StatisticsInsureNumRegionBo bo) {

        List<Nyt7PolicyInsurance> nyt7PolicyInsurances = FilterUtil.getInsurances(bo, nyt7PolicyInsuranceMapper, "region");
        if (nyt7PolicyInsurances == null) return new ArrayList<>();

        //总亩数
        BigDecimal totalMu = BigDecimal.valueOf(nyt7PolicyInsurances.stream()
            .mapToDouble(Nyt7PolicyInsurance::getTotalContractArea)
            .sum());

        List<Nyt7StatisticsInsureNumRegionVo> statisticsVos = new ArrayList<>();

        long i = 1;
        if (!nyt7PolicyInsurances.isEmpty()) {
            for (Nyt7PolicyInsurance nyt7PolicyInsurance : nyt7PolicyInsurances) {
                //面积
                BigDecimal area = BigDecimal.valueOf(nyt7PolicyInsurance.getTotalContractArea());
                //面积占比
                // 使用 BigDecimal 进行除法运算，并指定精度和舍入模式
                BigDecimal areaRatio = area
                    .divide(totalMu, 5, RoundingMode.HALF_UP);
                Nyt7StatisticsInsureNumRegionVo nyt7StatisticsInsureNumRegion = new Nyt7StatisticsInsureNumRegionVo();
                nyt7StatisticsInsureNumRegion.setId(i++);
                nyt7StatisticsInsureNumRegion.setRegion(nyt7PolicyInsurance.getPlotDetailInfo().getCity());
                nyt7StatisticsInsureNumRegion.setNextRegion(nyt7PolicyInsurance.getPlotDetailInfo().getDistrictCounty());
                nyt7StatisticsInsureNumRegion.setAreaAcres(area.toString());
                nyt7StatisticsInsureNumRegion.setAreaRatio(areaRatio);
                nyt7StatisticsInsureNumRegion.setCreateTime(nyt7PolicyInsurance.getCreateTime());
                statisticsVos.add(nyt7StatisticsInsureNumRegion);
            }
        }
        return statisticsVos;
    }

    /**
     * 查询符合条件的统计分析_投保统计_数量统计_按地区的数量统计列表
     *
     * @param bo 查询条件
     * @return 统计分析_投保统计_数量统计_按地区的数量统计列表
     */
    @Override
    public List<Nyt7StatisticsInsureNumRegionVo> queryList(Nyt7StatisticsInsureNumRegionBo bo) {
        return getStatisticsVos(bo);
    }
}
